package com.woniuxy.usercoachassemble.controller;

import com.alibaba.fastjson.JSON;
import com.woniuxy.usercoachassemble.entity.Message;
import com.woniuxy.usercoachassemble.entity.Userinfo;
import com.woniuxy.usercoachassemble.service.MessageService;
import com.woniuxy.usercoachassemble.service.UserinfoService;
import com.woniuxy.usercoachassemble.webservice.WebSocketServer;
import org.aspectj.weaver.ast.Var;
import org.example.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.xml.crypto.Data;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ClassName: WebSocketController
 * Description: webSocket
 * date: 2020/7/16 10:36
 *
 * @author Fy浪迹、
 */
@RestController
@RequestMapping("webSocketController")
public class WebSocketController {

    @Autowired
    private UserinfoService userinfoService;

    @Autowired
    private MessageService messageService;

    @RequestMapping("fs")
    public Map pushToWeb(HttpServletRequest request,String readId, String messageContent) {
        int userId = Integer.parseInt(request.getHeader("userId"));

        Map result = new HashMap();
        result.put("readId",readId);
        result.put("writeId",userId);
        result.put("messageTime",LocalDateTime.now());
        result.put("messageContent",messageContent);

        String message = JSON.toJSONString(result);
        try {
            WebSocketServer.sendInfo(message,readId);
            result.put("code", 200);
            result.put("msg", "success");
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 存入数据库
        Message message1 = new Message();
        message1.setReadId(Integer.parseInt(readId));
        message1.setWriteId(userId);
        message1.setMessageTime(LocalDateTime.now());
        message1.setMessageContent(messageContent);
        boolean save = messageService.save(message1);

        return result;
    }

    /**
     * @description:TODO(根据用户id查询用户详情)
     * @param userId
     * @return:  org.example.Result
     * @author: Fy浪迹、
     * @date: 2020/7/21 19:18
     */
    @GetMapping("getUserInfoById")
    public Result getUserInfoById(Integer userId){

        if(userId == null ){
            return Result.Error(600,"参数不能空");
        }
        Userinfo users = userinfoService.query().eq("user_id", userId).one();
        return Result.Success(users);
    }

    // 获取消息
    @GetMapping("getMessagesById")
    public Result getMessagesById(HttpServletRequest request,Integer userId){
        int userId2 = Integer.parseInt(request.getHeader("userId"));
        List<Message> list = messageService.query().eq("write_id", userId).eq("read_id", userId2).or().eq("write_id", userId2).eq("read_id", userId).list();
        return Result.Success(list);
    }


}
